package com.bkfm.baidu;

import java.util.HashMap;

import com.baidu.aip.ocr.AipOcr;
import com.bkfm.baidu.entity.TravelCard;

import org.json.JSONArray;
import org.json.JSONObject;


/**
 * 百度智能AI接口调用
 * @平台：BKFM统一开发平台
 * @文件名称：BaiDuAPIUtils.java
 * @文件描述：
 * @author XQ
 * @日期：2019年3月11日下午2:49:47
 *
 */
public class BaiDuAPIUtils extends BaiDuAction{

	/**
	 * 百度AI识别身份证照片，获取身份证信息,正面照片文字
	 * @param card_file_path
	 */
	public static String IDCardRecogizeFront(String card_file_path) {
		AipOcr client = new AipOcr(BAIDU_IMAGES_WORDS.AppID,BAIDU_IMAGES_WORDS.API_KEY, BAIDU_IMAGES_WORDS.SECRET_KEY);
		// 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("detect_risk", "false");
        
        JSONObject res = client.idcard(card_file_path, BAIDU_IMAGES_WORDS.IDCARDSIDE_FRONT, options);
        System.out.println(res.toString());
        return res.toString(2);
	}
	
	/**
	 * 百度AI识别身份证照片，获取身份证信息,正面照片文字
	 * @param card_file_path
	 */
	public static String IDCardRecogizeBack(String card_file_path) {
		AipOcr client = new AipOcr(BAIDU_IMAGES_WORDS.AppID,BAIDU_IMAGES_WORDS.API_KEY, BAIDU_IMAGES_WORDS.SECRET_KEY);
		// 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("detect_risk", "false");
        
        JSONObject res = client.idcard(card_file_path, BAIDU_IMAGES_WORDS.IDCARDSIDE_BACK, options);
        return res.toString(2);
	}
	
	/**
	 * 获取行驶证信息
	 * @param idCard_file_path
	 * @return
	 */
	public static TravelCard TravelCardRecogizeFront(String idCard_file_path) {
		AipOcr client = new AipOcr(BAIDU_IMAGES_WORDS.AppID,BAIDU_IMAGES_WORDS.API_KEY, BAIDU_IMAGES_WORDS.SECRET_KEY);
		HashMap<String, String> options = new HashMap<String, String>();
		options.put("detect_direction", "true");
	    options.put("probability", "true");
        JSONObject res = client.basicAccurateGeneral(idCard_file_path, options);
        System.out.println(res);
        JSONArray resultArray = res.getJSONArray("words_result");
        if(resultArray==null || resultArray.length()==0) {
        	return null;
        }
        TravelCard travelCard = new TravelCard();
        //travelCard.setCphm(resultArray.getString(index));
        return travelCard;
	}
}
